<template>
  <div>
    <head-text titleText="案件管理"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <Search :searchData="searchData" :searchItem="searchItem"></Search>
      </div>
      <div class="table-title">
        <h5>案件管理</h5>
      </div>
      <custom-table
        :tableData="tableData"
        :tableListData="tableListData"
        :tableBtn="tableBtn"
        :currentData="currentData"
        @handleEdit="handleEdit"
        @handleBackCase="handleBackCase"
        @onPageChange="onPageChange"
        @onSizeChange="onSizeChange"
      ></custom-table>
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList" :btnName.sync="btnName"></dialog-com>
  </div>
</template>

<script>
import headText from "@c/headText";
import customTable from "@c/table";
import Search from "@c/search";
import dialogCom from "./dialogCom";
import { get } from "lodash";
import {
  APPLYSTATUSENUM,
  APPLYTYPEENUM,
  APPLYSTATUSLIST,
  APPLYCATEGORYENUM
} from "@u/wordbook";
import { getApplyManagePageApi, getCustomerListApi,getSysuserSelectApi,applyCategoryListApi } from "@s/api";

export default {
  components: {
    headText,
    customTable,
    Search,
    dialogCom
  },
  data() {
    return {
      searchItem: [],
      searchData: {
        customerId: "",
        orderNumber: "",
        applyCategory: "",
        title: "",
        agentId: "",
        contractNumber: "",
        registerStartDate: "",
        registerEndDate: "",
        Status: ""
      },
      dialogObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      page: 1,
      total: 0,
      limit: 10,
      currentData: {},
      tableData: [],
      customerSelect: [], //客户名称
      isAgent: [], // 代理人
      userName: "",
      btnName: "编辑",
      applyCategoryList: [],
      statusSelect: APPLYSTATUSLIST
    };
  },
  created() {
    this.searchItem = [
      {
        type: "select",
        label: "客户名称：",
        prop: "customerId",
        placeholder: "请选择",
        selectList: this.customerSelect
      },
      {
        type: "select",
        label: "分类：",
        prop: "applyCategory",
        placeholder: "请选择",
        selectList: this.applyCategoryList
      },
      {
        type: "daterange",
        label: "立案日：",
        timeFormat: "yyyy-MM-dd",
        timeList: ["registerStartDate", "registerEndDate"]
      },
      {
        type: "input",
        label: "案卷号",
        prop: "orderNumber",
        placeholder: "请输入案卷号"
      },
      {
        type: "select",
        label: "代理人",
        prop: "agentId",
        placeholder: "请选择",
        selectList: this.isAgent
      },
      {
        type: "input",
        label: "项目名称",
        prop: "title",
        placeholder: "请输入项目名称"
      },
      {
        type: "input",
        label: "合同号",
        prop: "contractNumber",
        placeholder: "请输入合同号"
      },
      {
        type: "select",
        label: "状态：",
        prop: "Status",
        placeholder: "请选择",
        selectList: this.statusSelect
      }
    ];
    //  table表格
    this.tableListData = [ 
      { width: "50", label: "", type: "index" },
      { prop: "applyNumber", width: "", label: "编号" },
      { prop: "orderNumber", width: "", label: "案卷号" },
      { prop: "agentName", width: "90px", label: "代理人" },
      { prop: "contractNumber", width: "", label: "合同号" },
      { prop: "customerName", width: "", label: "客户名称" },
      { prop: "applyCategory", width: "", label: "分类",type: "wordbook", wordbookList: this.getApplyCategory },
      { prop: "applyType", width: "", label: "案件类型",type: "wordbook", wordbookList: this.getApplyType},
      { prop: "title", width: "100", label: "项目名称" },
      { prop: "registerDate", width: "100", type: "date", label: "分配时间" },
      { prop: "status", width: "100", label: "案件状态",type: "wordbook", wordbookList: this.getApplyStatus},
      { label: "操作", type: "btn", width: "180", fixed: "right" }
    ];
    // 按钮
    this.tableBtn = [
      {
        name: "编辑",
        btnType: "primary",
        handleFn: "handleEdit"
      }
    ];
  },
  mounted() {
    this.getList();
    this.getCustomerSelect();
    this.applyCategoryListHandle()
    this.getSysuserSelect();
    let userInfo = JSON.parse(localStorage.getItem("mb_userInfo"));
    const role = get( userInfo, "role", {})
    this.userName = role.name
    if (this.userName.indexOf("流程") != -1) {
      this.tableBtn.push({
        name: "驳回",
        btnType: "primary",
        handleFn: "handleBackCase"
      });
    }
  },
  methods: {
    //获取列表
    getList() {
      let json = JSON.parse(JSON.stringify(this.searchData));
      json.page = this.page;
      json.limit = this.limit;
      getApplyManagePageApi(json).then(res => {
        this.tableData = res.list;
        this.currentData.total = res.total;
      });
    },
    // 获取客户列表
    getCustomerSelect() {
      getCustomerListApi().then(res => {
        let newCustomerList = [];
        res.forEach(item => {
          var newItem = {
            id: item.id,
            value: item.customerName
          };
          newCustomerList.push(newItem);
        });
        this.customerSelect = newCustomerList;
        this.searchItem.forEach(item => {
          if (item.prop == "customerId") {
            item.selectList = this.customerSelect;
          }
        });
      });
    },
      applyCategoryListHandle() {
      applyCategoryListApi().then(res => {
        let newApplyCategoryList = [];
         res.forEach(item => {
          var newSettingItem = {
            id: item.settingValue,
            value: item.settingCode
          };
          newApplyCategoryList.push(newSettingItem);
        });
        this.applyCategoryList = newApplyCategoryList;
        this.searchItem.forEach(item => {
          if (item.prop == "applyCategory") {
            item.selectList = this.applyCategoryList;
          }
        });
      })
    },
    // 代理人
    getSysuserSelect() {
      getSysuserSelectApi({ name: "代理人" }).then((res) => {
       let newisAgentList = [];
        res.forEach(item => {
          var newItem = {
            id: item.userId,
            value: item.userName
          };
          newisAgentList.push(newItem);
        });
        this.isAgent = newisAgentList;
        this.searchItem.forEach(item => {
          if (item.prop == "agentId") {
            item.selectList = this.isAgent;
          }
        });
      });
    },
     getApplyCategory(val) {
      return APPLYCATEGORYENUM[val];
    },
    onPageChange(val) {
      this.page = val;
      this.getList();
    },
    onSizeChange(val) {
      this.page = 1;
      this.limit = val;
      this.getList();
    },
    // 搜索事件
    getDataList(val) {
      this.searchData = val;
      this.page = 1;
      this.limit = 10;
      this.$nextTick(() => {
        this.getList();
      });
    },
    handleBackCase(row) {
      this.btnName = "驳回";
      this.dialogObj.title = "驳回理由";
      this.dialogObj.id = row.id;
      this.dialogObj.show = true;
    },
     getApplyType(val) {
      return APPLYTYPEENUM[val];
    },
    getApplyStatus(val) {
      return APPLYSTATUSENUM[val];
    },
    // 编辑案件
    handleEdit(row) {
      this.dialogObj.id = row.id;
      this.dialogObj.show = true;
      this.dialogObj.form = row;
      this.btnName = "编辑";
      this.dialogObj.userName = this.userName;
      // row.status === "2" ? this.dialogObj.read = true : this.dialogObj.read = false
      this.dialogObj.title = "编辑案件";
    }
  }
};
</script>

<style lang="scss" scoped></style>
